package com.github.pudding.service.impl;

import com.github.pudding.model.Goods;
import com.github.pudding.model.Person;
import com.github.pudding.repostitory.GoodsRepository;
import com.github.pudding.repostitory.PersonRepository;
import com.github.pudding.service.PersonService;
import com.mongodb.MongoClient;
import com.mongodb.client.ClientSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service("personService")
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private GoodsRepository goodsRepository;

    @Autowired
    private MongoClient mongoClient;

    @Override
    public void insert() {


        ClientSession session = mongoClient.startSession();
        try {
            session.startTransaction();

            Person person = new Person();
            person.setId("5c09205f13758b1794ca99c4");
            person.setAge(24);
            person.setName("Jeff");
            person.setCity("London");
            personRepository.insert(person);

            Goods goods = new Goods();
            goods.setName("Laptop of Apple");
            goods.setDesc("Used for personal work!");
            goods.setOriginArea("California, USA");
            goods.setPrice(new BigDecimal("15893.00"));
            goods.setCurrency("CNY");
            goodsRepository.insert(goods);

            session.commitTransaction();
        } catch (Exception e) {
            e.printStackTrace();
            session.abortTransaction();
        }
    }
}
